﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
using System.Collections;
using System.IO.Ports;

namespace smartGardenForm
{
    public partial class smartGardenForm : Form
    {
        //实例化服务
        //smartGarden.SampleServiceSoapClient smartGardenService = new smartGarden.SampleServiceSoapClient();

        private dataDeal mydataDeal = new dataDeal();                            //实例化一个数据处理对象 
        private serialPort myserialPort = new serialPort();                      //实例化一个串口对象
        private dynamicAddControl mydynamicAddControl = new dynamicAddControl(); //实例化一个窗体处理类，用于动态添加窗体控件

        //系统加载窗体
        public smartGardenForm()
        {
            InitializeComponent();
        }



        //窗体载入事件
        private void addFac_Load(object sender, EventArgs e)
        {
            #region 通过服务获取所有  模式名称，下拉框 动态添加名称
            //string[] modeNames = smartGardenService.GetMode().ToArray();

            foreach (string modeName in modeNames)
            {
                if (!modeName.Contains("自定义"))
                {
                    comboBoxModeName.Items.Add(modeName.Trim());
                }
            }

            comboBoxModeName.SelectedIndex = 0;
            #endregion

            //serialPort对象串口下拉框添加串口名称项，波特率选项默认为  1152000方法 
            myserialPort.LoadSerialPortConfiguration(cmbPortName, comboBoxBaudRate);
        }

        #region 模式名称值改变时，从服务获取 对应模式的所有区域 并动态添加
        private void comboBoxModeName_SelectedIndexChanged(object sender, EventArgs e)
        {
            //清楚原有区域号
            comboBoxArea.Items.Clear();
            string modeName = comboBoxModeName.Text;

            //从服务动态获取所有区域号 并 添加
            int[] modeAreas = mysmartGardenService.GetModeAllArea(modeName).ToArray();
            foreach (int modeArea in modeAreas)
            {
                comboBoxArea.Items.Add(modeArea.ToString());
            }
            comboBoxArea.SelectedIndex = 0;
        }
        #endregion

        #region 区域号下拉框 改变时，动态添加所有  设备名称-开关-状态
        private void comboBoxArea_SelectedIndexChanged(object sender, EventArgs e)
        {
            string modeId = smartGardenService.GetModeId(comboBoxModeName.Text);
            //手动模式动态添加 标签 及开关按钮
            mydynamicAddControl.manModeAddControl(modeId, comboBoxArea.Text, groupBox1);

            //动态添加自动模式标签及文本框
            //autoModeAddControl(modeId);
        }
        #endregion

        #region 打开或关闭串口
        private void butOpenCom_Click(object sender, EventArgs e)
        {
            if (!serialPort1.IsOpen)
            {
                try
                {
                    serialPort1.PortName = cmbPortName.Text;                               //获取可用串口信息
                    serialPort1.BaudRate = int.Parse(comboBoxBaudRate.Text);               //设置波特率
                    serialPort1.DataBits = int.Parse("8");                                 //设置数据位
                    serialPort1.StopBits = (StopBits)Enum.Parse(typeof(StopBits), "One");  //设置停止位
                    serialPort1.Parity = (Parity)Enum.Parse(typeof(Parity), "None");       //设置校验位
                    serialPort1.Open();                                                    //打开串口

                    //在此处调用按钮函数
                    //ButtonEnableOrVisable("开启设备");
                    butOpenCom.Text = "关闭设备";
                }
                catch
                {
                    MessageBox.Show("串口未能打开，串口被占用或无法打开", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            }
            else
            {
                serialPort1.Close();                                                        //关闭串口
                //ButtonEnableOrVisable("关闭设备");
                butOpenCom.Text = "开启设备";
            }
        }
        #endregion
    }
}
